package com.huaqin.obeacons.utils;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

//一个监听手机摇晃的监听器
public class ShakeListener implements SensorEventListener {

	private static final String TAG = ShakeListener.class.getSimpleName();
	// 速度阈值，当摇晃速度达到这值后产生作用
	private static final int SPEED_SHRESHOLD = 1520;
	// 无效响应时间
	private static final int VALID_INTERVAL_TIME = 500;
	// 两次之间的时间间隔
	private static final int UPTATE_INTERVAL_TIME = 100;

	private SensorManager sensorManager;
	private Sensor sensor;
	// 重力感应监听器
	private OnShakeListener onShakeListener;
	private Context context;
	private float lastX;
	private float lastY;
	private float lastZ;

	private long lastUpdateTime;

	private int sValidShakingCount;
	private int sCurrentShakingCount;
	// 有效摇动的时间
	private long sValidShakingUpdateTime;

	// 构造器 获得监听对象
	public ShakeListener(Context c) {
		context = c;
	}

	public void startListening() {
		// 获取传感器管理器SensorManager
		sensorManager = (SensorManager) context
				.getSystemService(Context.SENSOR_SERVICE);
		if (sensorManager != null) {
			// 获取重力传感器对象sensor，TYPE_ACCELEROMETER对应加速度传感器
			sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		}
		if (sensor != null) {
			// 注册sensor服务，SENSOR_DELAY_GAME对应采样率20000微秒
			sensorManager.registerListener(this, sensor,
					SensorManager.SENSOR_DELAY_GAME);
		}

	}

	public void stopListening() {
		// 销毁sensor服务
		sensorManager.unregisterListener(this);
	}

	public interface OnShakeListener {
		public void onShake();
	}

	// 设置重力感应监听器
	public void setOnShakeListener(OnShakeListener listener) {
		onShakeListener = listener;
	}

	// 设置有效检测的次数
	public void setValidShakingCount(int count) {
		sValidShakingCount = count;
	}

	// 重力感应器感应获得变化数据
	public void onSensorChanged(SensorEvent event) {
		long currentUpdateTime = System.currentTimeMillis();
		long timeInterval = currentUpdateTime - lastUpdateTime;
		// 小于间隔时间
		if (timeInterval < UPTATE_INTERVAL_TIME) {
			return;
		}
		// 小于间隔时间
		if (currentUpdateTime - sValidShakingUpdateTime < UPTATE_INTERVAL_TIME * 2) {
			return;
		}
		//
		if (currentUpdateTime - sValidShakingUpdateTime > VALID_INTERVAL_TIME) {
			sCurrentShakingCount = 0;
			lastUpdateTime = currentUpdateTime;
			sValidShakingUpdateTime = lastUpdateTime;
			return;
		}
		// 更新lastUpdateTime
		lastUpdateTime = currentUpdateTime;
		// 获得x,y,z加速度
		float x = event.values[0];
		float y = event.values[1];
		float z = event.values[2];
		// 获得x,y,z的变化值
		float deltaX = x - lastX;
		float deltaY = y - lastY;
		float deltaZ = z - lastZ;
		// 将现在的数值变成last数值
		lastX = x;
		lastY = y;
		lastZ = z;

		double speed = Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ
				* deltaZ)
				/ timeInterval * 10000;
		// 达到阈值
		if (speed >= SPEED_SHRESHOLD) {
			sCurrentShakingCount++;
			sValidShakingUpdateTime = lastUpdateTime;
		}

		if (sCurrentShakingCount >= sValidShakingCount) {
			sCurrentShakingCount = 0;
			onShakeListener.onShake();
		}
	}

	// interface中抽象方法
	public void onAccuracyChanged(Sensor sensor, int accuracy) {

	}

	// 摇晃监听接口
	class OnShakeRunnable implements Runnable {

		@Override
		public void run() {
			onShakeListener.onShake();
		}
	}

	OnShakeRunnable mRunnbale = new OnShakeRunnable();
}